#include<stdio.h>
#include<stdlib.h> // malloc 函数
typedef struct{
    bool flag;
    char ch;
    int index;
}grid;

int main(){
    int kase = 0;
    do{
        int rows, cols;
        scanf("%d", &rows);
        if(!rows){
            break; // 退出程序
        }
        if(!rows) break;
        scanf("%d", &cols);

        int count = 0;
        // 分配字符数组空间
        grid ** p = (grid **)malloc(sizeof(grid *) * rows);
        for(int i = 0; i < rows; ++i){
            p[i] = (grid *)malloc(sizeof(grid) * cols);
            char * ch_ptr = (char *)malloc(sizeof(char) * cols);
            scanf("%s", ch_ptr);
            for(int j = 0; j < cols; ++j){
                p[i][j].ch = ch_ptr[j];
               if(i == 0 || j == 0 || p[i][j - 1].ch == '*' || p[i - 1][j].ch == '*'){
                    if(p[i][j].ch != '*'){
                        p[i][j].flag = true;
                        ++count;
                        p[i][j].index = count;
                    }else{
                        p[i][j].flag = false;
                        p[i][j].index = 0;
                    }
                }else{
                    p[i][j].flag = false;
                    p[i][j].index = 0;
                }
            }
            free(ch_ptr);
        }

        if(kase){
            printf("\n");
        }
        printf("puzzle #%d:\n", ++kase);
        printf("Across\n");
        for(int row = 0; row < rows; ++row){
            for(int col = 0; col < cols; ++col){
                if(p[row][col].flag){
                    printf("%3d.%c", p[row][col].index, p[row][col].ch);
                    do{
                        ++col;
                        if(col < cols && p[row][col].ch != '*'){
                            printf("%c", p[row][col].ch);
                        }else{
                            break;
                        }
                    }while(1);
                    printf("\n");
                }
            }
        }

        printf("Down\n");
        int* index = (int*)malloc(sizeof(int) * cols);
        for(int i = 0; i < cols; ++i){
            index[i] = 0;
        }

        while(true){
            int min = 0;
            for(int i = 0; i < cols; ++i){
                if(i == 0){
                    min = i;
                    continue;
                }
                if(index[i] < index[min]){
                    min = i;
                }else{
                    continue;
                }
            }
            if(min >= cols || index[min] >= rows){
                break; // 退出结束标志
            }
            int col = min;
            int row = index[min];
            if(row < rows && p[row][col].flag){
                printf("%3d.%c", p[row][col].index, p[row][col].ch);
                do{
                    ++row;
                    if((row < rows) && (p[row][col].ch != '*')){
                        printf("%c", p[row][col].ch);
                    }else{
                        break;
                    }
                }while(1);
                printf("\n");
            }
            index[col] = row < rows ? (row + 1):(rows);
        }

        free(index);
        for(int i = 0; i < rows; ++i){
            free(p[i]);
        }
        free(p);
    }while(1);
}
